package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.entity.User;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aming
 * @since 2025-01-19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer id,
                       @RequestParam(required = false) String account,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer gender,
                       @RequestParam(required = false) Integer deptId,
                       @RequestParam(required = false) Integer roleId) {
        try {
            User user = new User();
            if (id != null){
                user.setId(Long.valueOf(id));
            }
            user.setAccount(account);
            user.setName(name);
            user.setGender(gender);
            user.setDeptId(deptId);
            user.setRoleId(roleId);
            PageBean pageBean = userService.page(page, pageSize, user);
            return Result.success(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @PostMapping
    @Transactional
    public Result addUser(@RequestBody User user) {
        try {
            int result = userService.addUser(user);
            if (result == 0) {
                return Result.error("账号已存在");
            }
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @PutMapping
    @Transactional
    public Result updateUser(@RequestBody User user) {
        try {
            int result = userService.updateUser(user);
            if (result == 0){
                return Result.error("修改失败");
            }else {
                return Result.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @DeleteMapping("/{ids}")
    @Transactional
    public Result deleteUser(@PathVariable List<Integer> ids) {
        try {
            int result = userService.deleteUser(ids);
            if (result == 0) {
                return Result.error("删除失败");
            } else {
                return Result.success("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
}